package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.constant.WareConstant;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.vo.MergeVo;
import com.atguigu.gulimall.ware.vo.PurchaseDetailDoneVo;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.cert.CollectionCertStoreParameters;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDao;
import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService purchaseDetailService;


    @Autowired
    WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {

        QueryWrapper<PurchaseEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("status", 0).or().eq("status", 1);


        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId == null) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED_STATUS.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        List<Long> items = mergeVo.getItems();
        Long tempId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map((item -> {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            detailEntity.setId(item);
            detailEntity.setPurchaseId(tempId);
            detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED_STATUS.getCode());
            return detailEntity;
        })).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(collect);

    }

    @Override
    public void savePurchase(PurchaseEntity purchase) {
        purchase.setUpdateTime(new Date());
        purchase.setCreateTime(new Date());
        this.save(purchase);
    }

    @Override
    public void received(List<Long> ids) {

        //确认采购单状态是否是已分配或者新建
        List<PurchaseEntity> entities = ids.stream().map((this::getById)).filter(item -> {
            return (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED_STATUS.getCode() || item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED_STATUS.getCode());
        }).peek(item -> {
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE_STATUS.getCode());
            item.setUpdateTime(new Date());
        }).collect(Collectors.toList());


        //改变采购单状态
        this.updateBatchById(entities);

        //改变采购项状态
        entities.forEach(item -> {
            List<PurchaseDetailEntity> list = purchaseDetailService.listDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> collect = list.stream().map(entitie -> {
                PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
                detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING_STATUS.getCode());
                detailEntity.setId(entitie.getId());
                return detailEntity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect);
        });

    }


    @Transactional
    @Override
    public void done(PurchaseDoneVo doneVo) {
        Long id = doneVo.getId();

        List<Boolean> list = new ArrayList<>();
        List<PurchaseDetailDoneVo> items = doneVo.getItems();
        List<PurchaseDetailEntity> collect = items.stream().map((item -> {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            if (item.getStatus().equals(WareConstant.PurchaseDetailStatusEnum.BUYING_ERROR_STATUS.getCode())) {
                list.add(false);
            }else {
                PurchaseDetailEntity byId = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addStock(byId.getSkuId(),byId.getWareId(),byId.getSkuNum());
            }
            detailEntity.setStatus(item.getStatus());
            detailEntity.setId(item.getItemId());
            return detailEntity;
        })).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(collect);


        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(list.size() > 0 ?
                WareConstant.PurchaseStatusEnum.HAS_ERROR_STATUS.getCode()
                : WareConstant.PurchaseStatusEnum.FINISH_STATUS.getCode());
        this.updateById(purchaseEntity);

    }

}